<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use DB;
use App\Model\News;
use Auth;
use Captcha;
use Validator;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{


    //验证码
    public function getCode()
    {
// 		echo captcha_img();
        echo captcha_src();
    }

    public function getVc()
    {
        dd(Captcha::check('POLK'));
    }

    public function getMk()
    {
        $str="## h1";
        echo Markdown::convertToHtml($str);
    }




    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //第一种DB方式
        /* //分页
        $data = DB::table('news')->orderBy('id','desc')->paginate(3);
        //显示主列表页面
        return view('news.list',['data'=>$data]); */

        //第二种方式 使用model
        $data = News::orderBy('id','desc')->paginate(5);
        return view('news.list',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //显示添加页面
        return view('news.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * request 对象
     */
    public function store(Request $request)
    {
        //第一种DB方式
        /* //执行添加
        $data = $request->except('_token');
        $res = DB::table('news')->insert($data);

        if($res){
            return redirect('/news');
        }else{
            return back();
        } */

        //第二种方式 model
        // 1 orm 第一种入库方式
        /* $data = new News();
        //获取数据
        $data->title = $request->title;
        $data->content = $request->content;
        $data->save();//保存到库
        return redirect('/news'); */

        //验证  三个参数 （数据 规则 message信息）所有表单验证都可添加
        //门面validate的验证方式
        $val =  Validator::make($request->all(), [
            'title' => 'required|max:30|min:8',
            'content' => 'required|max:1000|min:50',
        ]);

        //没有通过验证
        if($val->fails()){
            return back()->withErrors($val)->withInput();
        }

        // 2 orm 第二种入库方式 create
        $data = News::create(['title'=>$request->title,'content'=>$request->content]);
        //上面语句等同于		$data = News::create($request->except('_token'));
        return redirect('/news');
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //点击阅读详情  显示详情页

        //第一种DB方式
        /*  $detail = DB::table('news')->where('id','=',$id)->first();
            // 等同于 $detail = DB::table('news')->find($id);
         return view('news.read',['detail'=>$detail]); */

        //第二种方式 model
        $detail = News::find($id);
        return view('news.read',['detail'=>$detail]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 内容修改修改显示页面 $datas 命名不要和foreach重复

        //第一种DB方式
        /* $datas = DB::table('news')->find($id);
        return view('news.edit',['datas'=>$datas]); */

        //第二种方式 model
        $datas = News::find($id);
        return view('news.edit',['datas'=>$datas]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //执行数据修改

        //第一种DB方式
        /* $res = DB::table('news')->where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
		//update 用redirect
        return redirect('/news'); */


        //第二种方式 model
        // 1 orm 第一种入库方式
        /* $res = News::find($id);
        //获取数据
        $res->title = $request->title;
        $res->content = $request->content;
        $res->save();//保存到库
        return redirect('/news'); */

        // 2 orm 第二种入库方式 create
        $data = News::where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
        //上面语句等同于	$data = News::where('id',$id)->update($request->except('_token','_method'));
        return redirect('/news');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        /* $data = DB::table('news')->delete(['id'=>$id]);
        return redirect('/news'); */

        //第二种方式 model
        $data = News::find($id)->delete();
        return redirect('/news');
    }

    public  function  getUser()
    {
        //用户登录成功获取用户信息  未登录为null
        dd(Auth::user());

        //验证用户 登录成功 密码正确返回TRUE 否则false
//     	dd(Auth::check());

    }

    //自己建立退出方法  调用 http://bookk.com/udetail/logout
    public function  getLogout()
    {
        Auth::logout();
        return back();
    }

    //自己定义手动登录
    public function postLogin(Request $request)
    {
        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);


        if($res){
            return redirect('/news');
        }else{
            return back();
        }
    }
}

